from connection import db
from common.resp import respSuccessJson, respErrorJson
from common import error_code
from models.users import User
from fastapi import APIRouter, HTTPException
import sys
sys.path.append("..")
# from controllor.users import add

collection = db.user

router = APIRouter(prefix="/users")


@router.post("/add")
async def add(item: User):
    user = {'username': item.username, 'password': item.password}
    ret = collection.find_one({'username': item.username})
    if ret:
        return respErrorJson(error=error_code.ERROR_USER_EXIST)
    else:
        collection.insert_one(user)
        return respSuccessJson(data=None, msg='恭喜您，注册成功！')